package com.jbit.dm212.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.jbit.dm212.common.DmObjectUtil;
import com.jbit.dm212.common.PageInfoDTO;
import com.jbit.dm212.common.PageInfoUtils;
import com.jbit.dm212.entity.dto.DmItemDTO;
import com.jbit.dm212.entity.dto.DmItemDetailDTO;
import com.jbit.dm212.entity.dto.DmItemSearchDTO;
import com.jbit.dm212.entity.dto.FloorItemDTO;
import com.jbit.dm212.entity.po.*;
import com.jbit.dm212.mapper.DmItemMapper;
import com.jbit.dm212.service.DmItemService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DmItemServiceImpl implements DmItemService {
    @Resource
    private DmItemMapper dmItemMapper;

    @Override
    public List<DmItem> getHotItemsByTypeId(Integer typeId) {
        MPJLambdaWrapper<DmItem> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.select(DmItem::getId, DmItem::getMinPrice, DmItem::getItemName);
        lambdaWrapper.eq(DmItem::getItemType1Id, typeId);
        lambdaWrapper.last("limit 5");
        return dmItemMapper.selectList(lambdaWrapper);
    }

    @Override
    public List<DmItemDTO> getBanner() {
        MPJLambdaWrapper<DmItem> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.select(DmItem::getId, DmItem::getItemName, DmItem::getMinPrice);
        lambdaWrapper.select(DmImage::getImgUrl);
        lambdaWrapper.innerJoin(DmImage.class, DmImage::getTargetId, DmItem::getId);
        lambdaWrapper.eq(DmItem::getIsBanner, 1);
        lambdaWrapper.eq(DmImage::getType,1);
        lambdaWrapper.eq(DmImage::getCategory,1);
        lambdaWrapper.last("limit 5");
        lambdaWrapper.orderByDesc(DmItem::getStartTime);

        return dmItemMapper.selectJoinList(DmItemDTO.class, lambdaWrapper);
    }

    @Override
    public List<DmItemDTO> getTodayRecommend() {
        MPJLambdaWrapper<DmItem> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.select(DmItem::getId, DmItem::getItemName, DmItem::getMinPrice);
        lambdaWrapper.select(DmImage::getImgUrl);
        lambdaWrapper.innerJoin(DmImage.class, DmImage::getTargetId, DmItem::getId);
        lambdaWrapper.eq(DmItem::getIsRecommend, 1);
        lambdaWrapper.last("limit 5");
        lambdaWrapper.orderByDesc(DmItem::getStartTime);
        return dmItemMapper.selectJoinList(DmItemDTO.class, lambdaWrapper);
    }

    @Override
    public List<DmItemDTO> getToSaleItem() {
        MPJLambdaWrapper<DmItem> lambdaWrapper = queryHomeDmItem();
        lambdaWrapper.eq(DmItem::getState, 2);
        lambdaWrapper.last("limit 5");
        lambdaWrapper.orderByDesc(DmItem::getStartTime);
        return dmItemMapper.selectJoinList(DmItemDTO.class, lambdaWrapper);
    }

    @Override
    public List<FloorItemDTO> getFloorItems(Integer typeId) {
        MPJLambdaWrapper<DmItem> lambdaWrapper = new MPJLambdaWrapper<>();
        //DmItem的信息
        lambdaWrapper.select(DmItem::getId, DmItem::getItemName, DmItem::getMinPrice);
        lambdaWrapper.selectAs(DmItem::getStartTime, "startDate");
        lambdaWrapper.selectAs(DmItem::getEndTime, "endDate");
        lambdaWrapper.eq(DmItem::getItemType1Id, typeId);
        lambdaWrapper.last("limit 5");
        //DmCinema
        lambdaWrapper.select(DmCinema::getAreaId, DmCinema::getAddress, DmCinema::getAreaName);
        lambdaWrapper
                .innerJoin(DmScheduler.class, DmScheduler::getItemId, DmItem::getId)
                .innerJoin(DmCinema.class, DmCinema::getId, DmScheduler::getCinemaId);
        //DmImage
        lambdaWrapper.select(DmImage::getImgUrl);
        lambdaWrapper.leftJoin(DmImage.class, DmImage::getTargetId, DmItem::getId);
        lambdaWrapper.distinct();
        //scheduler cinema image

        return dmItemMapper.selectJoinList(FloorItemDTO.class, lambdaWrapper);
    }

    @Override
    public List<FloorItemDTO> getHotItems(Integer typeId) {
        MPJLambdaWrapper<DmItem> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.select(DmItem::getId, DmItem::getItemName);
        lambdaWrapper.select(DmCinema::getAreaId, DmCinema::getAreaName);
        lambdaWrapper.eq(DmItem::getIsRecommend, 1);
        lambdaWrapper.eq(DmItem::getItemType1Id, typeId);
        lambdaWrapper.last("limit 13");
        lambdaWrapper.distinct();
        lambdaWrapper
                .innerJoin(DmScheduler.class, DmScheduler::getItemId, DmItem::getId)
                .innerJoin(DmCinema.class, DmCinema::getId, DmScheduler::getCinemaId);

        return dmItemMapper.selectJoinList(FloorItemDTO.class, lambdaWrapper);
    }

    @Override
    public PageInfoDTO<DmItemSearchDTO> getItemList(DmItem item, DmArea area, CommonData data) {
        MPJLambdaWrapper<DmItem> lambdaWrapper = JoinWrappers.lambda();
        lambdaWrapper.select(
                DmItem::getId,
                DmItem::getItemName,
                DmItem::getAbstractMessage,
                DmItem::getStartTime,
                DmItem::getEndTime,
                DmItem::getMaxPrice,
                DmItem::getMinPrice
        );

        lambdaWrapper.select(
                DmCinema::getAreaName,
                DmCinema::getAddress,
                DmCinema::getLatitude,
                DmCinema::getLongitude
        );

        lambdaWrapper.select(DmImage::getImgUrl);

        lambdaWrapper.leftJoin(DmImage.class, DmImage::getTargetId, DmItem::getId)
                .innerJoin(DmScheduler.class, DmScheduler::getItemId, DmItem::getId)
                .innerJoin(DmCinema.class, DmCinema::getId, DmScheduler::getCinemaId);

        lambdaWrapper.distinct();


        lambdaWrapper.eq(DmObjectUtil.isNotEmpty(item.getItemType1Id()), DmItem::getItemType1Id, item.getItemType1Id());
        if (DmObjectUtil.isNotEmpty(item.getItemType2Id()))
            lambdaWrapper.eq(DmItem::getItemType2Id, item.getItemType2Id());
        if (DmObjectUtil.isNotEmpty(area.getId()))
            lambdaWrapper.eq(DmCinema::getAreaId, area.getId());
        if (item.getStartTime() != null)
            lambdaWrapper.eq(DmItem::getStartTime, item.getStartTime());
        if (item.getEndTime() != null)
            lambdaWrapper.eq(DmItem::getEndTime, item.getEndTime());
        if (StringUtils.isNotEmpty(item.getItemName()))
            lambdaWrapper.like(DmItem::getItemName, item.getItemName());

        if (data.getSort().equals("recommend")) {
            lambdaWrapper.orderByDesc(DmItem::getIsRecommend);
        } else if (data.getSort().equals("recentShow")) {
            lambdaWrapper.orderByDesc(DmItem::getStartTime);
            lambdaWrapper.eq(DmItem::getState, 3);
        } else if (data.getSort().equals("recentSell")) {
            lambdaWrapper.orderByDesc(DmItem::getStartTime);
            lambdaWrapper.eq(DmItem::getState, 2);
        }

        Page<DmItemSearchDTO> page = Page.of(data.getCurrentPage(), 10);
        page = dmItemMapper.selectJoinPage(page, DmItemSearchDTO.class, lambdaWrapper);
        return PageInfoUtils.convertPageToPageInfo(page);
    }

    @Override
    public DmItemDetailDTO getDetailById(Integer itemId) {
        MPJLambdaWrapper<DmItem> lambdaWrapper = new MPJLambdaWrapper<>();
        //DmItem
        lambdaWrapper.select(
                DmItem::getId,
                DmItem::getAbstractMessage,
                DmItem::getItemName,
                DmItem::getItemType1Id,
                DmItem::getItemType1Name,
                DmItem::getItemType2Id,
                DmItem::getItemType2Name,
                DmItem::getState,
                DmItem::getBasicDescription,
                DmItem::getProjectDescription,
                DmItem::getReminderDescription,
                DmItem::getCommentCount,
                DmItem::getAvgScore,
                DmItem::getCinemaId,
                DmItem::getStartTime,
                DmItem::getEndTime
        );
        //DmCinama
        lambdaWrapper.select(
                DmCinema::getAreaId,
                DmCinema::getAreaName,
                DmCinema::getAddress,
                DmCinema::getLongitude,
                DmCinema::getLatitude
        );
        //DmImage
        lambdaWrapper.select(DmImage::getImgUrl);

        lambdaWrapper
                .leftJoin(DmImage.class,DmImage::getTargetId,DmItem::getId)
                .innerJoin(DmScheduler.class,DmScheduler::getItemId,DmItem::getId)
                .innerJoin(DmCinema.class,DmCinema::getId,DmScheduler::getCinemaId);

        lambdaWrapper.eq(DmItem::getId,itemId);
        lambdaWrapper.distinct();

        return dmItemMapper.selectJoinOne(DmItemDetailDTO.class,lambdaWrapper);
    }

    @Override
    public DmItem getItemById(Integer id) {
        MPJLambdaWrapper<DmItem> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.eq(DmItem::getId,id);
        return dmItemMapper.selectOne(lambdaWrapper);
    }

    public MPJLambdaWrapper queryHomeDmItem() {
        MPJLambdaWrapper<DmItem> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.select(DmItem::getId, DmItem::getItemName, DmItem::getMinPrice);
        lambdaWrapper.select(DmImage::getImgUrl);
        lambdaWrapper.innerJoin(DmImage.class, DmImage::getTargetId, DmItem::getId);
        return lambdaWrapper;
    }
}
